package 查找算法;

/**
 * Created by hest0 on 2017/4/21.
 */
public class 二分查找 {
    public static void main(String[] args) {

    }

    public class BinarySearch {
        public int getPos(int[] a, int x) {
            int l = 0;
            int r = a.length - 1;
            if (x == a[0])
                return 0;
            while (l <= r) {
                int m = (l + r) / 2;
                if (x == a[m]) {
                    return m;
                } else if (x < a[m]) {
                    r = m - 1;
                } else {
                    l = m + 1;
                }
            }
            return -1;
        }
    }

}
